<?php

namespace App\Http\Controllers\V1\Admin;

use App\Models\V1\SysPermission;
use Illuminate\Http\Request;

class MenuController extends BaseController
{

    /**
     * 菜单列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request){
        $query=SysPermission::query();
        if($request->filled('name')){
            $query->where("name","like","%".$request->input('name')."%");
        }
        if($request->filled('state')){
            $query->where('state',$request->input('state'));
        }

        $list=$query->orderBy('order_num','asc')->get()->toArray();
        return ajax_output(200,"成功",$list);
    }

    /**
     * 保存菜单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $menu_model=new SysPermission();
        $menu_model->create([
            'parent_id'=>$request->input('parent_id',0),
            'name'=>$request->input('name',""),
            'icon'=>$request->input('icon',""),
            'is_cache'=>$request->input('is_cache',0),
            'is_frame'=>$request->input('is_frame',0),
            'query'=>$request->input('query',0),
            'menu_type'=>$request->input('menu_type',""),
            'order_num'=>$request->input('order_num',0),
            'path'=>$request->input('path',""),
            'state'=>$request->input('state',1),
            'component'=>$request->input('component',""),
            'perms'=>$request->input('perms',""),
            'visible'=>$request->input('visible',0),
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 菜单详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id)
    {
        $menu = SysPermission::findOrFail($id);
        return ajax_output(200,"成功",$menu);
    }

    /**
     * 更新菜单
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, $id)
    {
        $menu = SysPermission::findOrFail($id);
        $menu->update([
            'parent_id'=>$request->input('parent_id',0),
            'name'=>$request->input('name',""),
            'icon'=>$request->input('icon',""),
            'is_cache'=>$request->input('is_cache',0),
            'is_frame'=>$request->input('is_frame',0),
            'query'=>$request->input('query',0),
            'menu_type'=>$request->input('menu_type',""),
            'order_num'=>$request->input('order_num',0),
            'path'=>$request->input('path',""),
            'state'=>$request->input('state',1),
            'component'=>$request->input('component',""),
            'perms'=>$request->input('perms',""),
            'visible'=>$request->input('visible',0),
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 删除菜单
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        SysPermission::destroy($id);
        return ajax_output(200,"成功");
    }

    /**
     * 菜单树
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function treeselect(Request $request){
        //查询所有的权限
        $permission_model=new SysPermission();
        $allpermissions=$permission_model->select(['id','name as label','parent_id'])->get()->toArray();
        $data=genTree($allpermissions,'parent_id');
        return ajax_output(200,"成功",$data);
    }
}
